<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 24.3.2</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="24.3.1.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="25.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P4">Part IV. The C API</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#24">Chapter 24. An Overview of the C API</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<p><h3>24.3.2 &ndash; Error Handling in Library Code</h3>

<p>Lua is a <em>safe</em> language.
That means that, no matter what you write,
no matter how wrong it is,
you can always understand the behavior of a program
in terms of Lua itself.
Moreover, errors are detected and explained in terms of Lua, too.
You can contrast that with C,
where the behavior of many wrong programs
can only be explained in terms of the underling hardware
and error positions are given as a program counter.

<p>Whenever you add new C functions to Lua,
you can break that safety.
For instance, a function like <code>poke</code>,
which stores an arbitrary byte at an arbitrary memory address,
can cause all sorts of memory corruption.
You must strive to ensure that your add-ons are safe to Lua
and provide good error handling.

<p>As we discussed earlier,
each C program has its own way to handle errors.
When you write library functions for Lua, however,
there is a standard way to handle errors.
Whenever a C function detects an error,
it simply calls <code>lua_error</code>,
(or better yet <code>luaL_error</code>,
which formats the error message and then
calls <code>lua_error</code>).
The <code>lua_error</code> function clears
whatever needs to be cleared in Lua
and jumps back to the
<code>lua_pcall</code> that originated that execution,
passing along the error message.

<p>
<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="25.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

